package com.zhangwei.liwushuo.utils;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.TextHttpResponseHandler;
import com.zhangwei.liwushuo.bean.AppEntity;
import com.zhangwei.liwushuo.bean.Data;
import com.zhangwei.liwushuo.config.AppConfig;
import com.zw.library.adapter.AAdapter;
import com.zw.library.utils.Abus;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.apache.http.Header;

/**
 * @说 明： 联网工具类
 * @项目名称：LiWuShuo
 * @包 名： com.zhangwei.liwushuo.utils
 * @类 名： AsyncHttpHelper
 * @创 建人： zhangwei
 * @创建时间：2015-08-18 00:13
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
@SuppressWarnings("deprecation")
@EBean
public class AsyncHttpHelper {
    public static AsyncHttpHelper helper;
    private AsyncHttpClient httpClient;
    @Bean
    Abus abus;

    public AsyncHttpHelper() {
        httpClient = new AsyncHttpClient();
    }

    public <T> RequestHandle get(String url, final AAdapter<T> adapter, final PullToRefreshBase ptrListView, final TypeReference<AppEntity<Data<T>>> type, final int mCurrentPage) {
        @SuppressWarnings("deprecation")
        final RequestHandle get = httpClient.get(url, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCde, @SuppressWarnings("deprecation") Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, @SuppressWarnings("deprecation") Header[] headers, String responseString) {
                AppEntity<Data<T>> entity = JSONObject.parseObject(responseString, type);

                if (entity != null && entity.getCode() == 200 && AppConfig.REQUEST_STATE_OK.equals(entity.getMessage()) && entity.getData() != null) {

                    if (mCurrentPage == 0 && adapter.getData().size() > 0) {
                        adapter.getData().clear();
                    }
                    adapter.addData(entity.getData().getItems());
                }
                ptrListView.onRefreshComplete();
                String url = entity.getData().getPaging().getNext_url();
                if (!TextUtils.isEmpty(url)) {
                    abus.post(url);
                } else {
                    ptrListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                }
            }
        });
        return get;
    }

    /**
     * @param url
     * @param adapter
     * @param ptrListView
     * @param cls
     * @param mCurrentPage
     * @param <T>
     * @return
     */
    public <T> RequestHandle get(String url, final AAdapter<T> adapter, final PullToRefreshBase ptrListView, final AppEntity<Data<T>> cls, final int mCurrentPage) {
        @SuppressWarnings("deprecation")
        final RequestHandle get = httpClient.get(url, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCde, @SuppressWarnings("deprecation") Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, @SuppressWarnings("deprecation") Header[] headers, String responseString) {


                AppEntity<Data<T>> entity = JSONObject.parseObject(responseString, cls.getClass().getGenericSuperclass());
                if (entity != null && entity.getCode() == 200 && AppConfig.REQUEST_STATE_OK.equals(entity.getMessage()) && entity.getData() != null) {

                    if (mCurrentPage == 0 && adapter.getData().size() > 0) {
                        adapter.getData().clear();
                    }
                    adapter.addData(entity.getData().getItems());
                }
                ptrListView.onRefreshComplete();
                String url = entity.getData().getPaging().getNext_url();
                if (!TextUtils.isEmpty(url)) {
                    abus.post(url);
                } else {
                    ptrListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                }
            }
        });
        return get;
    }

    public <T> RequestHandle get(String url, final Class<T> cls) {
        @SuppressWarnings("deprecation")
        final RequestHandle get = httpClient.get(url, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCde, @SuppressWarnings("deprecation") Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, @SuppressWarnings("deprecation") Header[] headers, String responseString) {

                AppEntity<Data<T>> entity = (AppEntity<Data<T>>) JSONObject.parseObject(responseString, cls);

            }
        });
        return get;
    }

//    public <T> TypeReference<T> getClassType(final Class<T> cls) {
//
//        TypeReference<T> type = new TypeReference<AppEntity<Data<cls>>>() {
//        };
//
//
//    }


}
